#include "TCanvas.h"
#include "TLorentzVector.h"
#include "TSystem.h"
#include "TCut.h"
#include "TString.h"
#include "TH1F.h"
#include "TLegend.h"
#include "TChain.h"
#include "TGraphAsymmErrors.h"
#include "iostream"


using namespace RooFit;

void StripCut()
{
    TString fname = "./Feature.root";
    TFile *input = new TFile(fname);
    TTree *t_data = (TTree*)input->Get("t_data");

    double sumEvnets1 = 0, sumEvnets2 = 0;
    double maxevent1 = 0, maxevent2 = 0;

    // TString ofcutname;
    // ofcutname = "cut.txt";
    // ofstream ofcut;
    // ofcut.open(ofcutname);
    
    TString opdfname;
    opdfname = "cut.pdf";
    
    Int_t data[72][72];  t_data->SetBranchAddress("data",&data);
    Int_t numEvent;      t_data->SetBranchAddress("numEvent",&numEvent);
    Int_t numFrame;      t_data->SetBranchAddress("numFrame",&numFrame);
    Int_t timeYear;      t_data->SetBranchAddress("timeYear",&timeYear);
    Int_t timeMon;       t_data->SetBranchAddress("timeMon",&timeMon);
    Int_t timeMday;      t_data->SetBranchAddress("timeMday",&timeMday);
    Int_t timeHour;      t_data->SetBranchAddress("timeHour",&timeHour);
    Int_t timeMin;       t_data->SetBranchAddress("timeMin",&timeMin);
    Int_t timeSec;       t_data->SetBranchAddress("timeSec",&timeSec);
    Int_t timeNsec;      t_data->SetBranchAddress("timeNsec",&timeNsec);
    Int_t peakADC;       t_data->SetBranchAddress("peakADC",&peakADC);
    Double_t peakX;      t_data->SetBranchAddress("peakX",&peakX);
    Double_t peakY;      t_data->SetBranchAddress("peakY",&peakY);
    Int_t sumADC;        t_data->SetBranchAddress("sumADC",&sumADC);
    Int_t sumHit;        t_data->SetBranchAddress("sumHit",&sumHit);
    Double_t lenXY;      t_data->SetBranchAddress("lenXY",&lenXY);
    Double_t ratioXY;    t_data->SetBranchAddress("ratioXY",&ratioXY);
    Double_t BcenterX;   t_data->SetBranchAddress("BcenterX",&BcenterX);
    Double_t BcenterY;   t_data->SetBranchAddress("BcenterY",&BcenterY);
    Double_t lenBtoP;    t_data->SetBranchAddress("lenBtoP",&lenBtoP);
    Int_t length;        t_data->SetBranchAddress("length",&length);
    Double_t width;      t_data->SetBranchAddress("width",&width);
    Int_t outline;       t_data->SetBranchAddress("outline",&outline);
    Int_t edgeHit;       t_data->SetBranchAddress("edgeHit",&edgeHit);
    //
    //-------------------CUTs---------------------------------
    //
    //Trigger cut
    TCut cut_numEvent  =  "";
    TCut cut_numFrame  =  "numFrame == 2";
    TCut cut_timeYear  =  "";
    TCut cut_timeMon   =  "";
    TCut cut_timeMday  =  "";
    TCut cut_timeHour  =  "";
    TCut cut_timeMin   =  "";
    TCut cut_timeSec   =  "";
    TCut cut_timeNsec  =  "";
    TCut cut_peakADC   =  "";
    TCut cut_peakX     =  "";
    TCut cut_peakY     =  "";
    TCut cut_sumADC    =  "";
    TCut cut_sumHit    =  "";
    TCut cut_lenXY     =  "";
    TCut cut_ratioXY   =  "";
    TCut cut_BcenterX  =  "";
    TCut cut_BcenterY  =  "";
    TCut cut_lenBtoP   =  "";
    TCut cut_length    =  "";
    TCut cut_width     =  "";
    TCut cut_outline   =  "";
    TCut cut_edgeHit   =  "edgeHit < 4";
    
    //
    //data cut
    //
    TCut cut_data;
    cut_data += cut_numEvent;
    cut_data += cut_numFrame;
    cut_data += cut_timeYear;
    cut_data += cut_timeMon;
    cut_data += cut_timeMday;
    cut_data += cut_timeHour;
    cut_data += cut_timeMin;
    cut_data += cut_timeSec;
    cut_data += cut_timeNsec;
    cut_data += cut_peakADC;
    cut_data += cut_peakX;
    cut_data += cut_peakY;
    cut_data += cut_sumADC;
    cut_data += cut_sumHit;
    cut_data += cut_lenXY;
    cut_data += cut_ratioXY;
    cut_data += cut_BcenterX;
    cut_data += cut_BcenterY;
    cut_data += cut_lenBtoP;
    cut_data += cut_length;
    cut_data += cut_width;
    cut_data += cut_outline;
    cut_data += cut_edgeHit;
    
    //cut_data = "";
    
    
    //
    //-----------------END CUTs--------------------------------
    //
    cout<<cut_data.GetTitle()<<endl;
    
    //gROOT->cd();
    TFile* ouput = TFile::Open("frame2_egde4.root","RECREATE");
    TTree *t_cut = t_data->CopyTree(cut_data);
    // t_cut->SetName(cut_data.GetTitle());
    t_cut->SetName("t_data");
    t_cut->Write();
    // t_cut->Print();

    //TH1F *hist = new TH1F("hist","strip cut histogram",100,0,200000);
    Double_t strptcutsum;
    for(int i=0; i<t_cut->GetEntries(); i++)
    {
        t_cut->GetEntry(i);
        if(sumEvnets1 != numEvent)
        {
            maxevent1 = numEvent;
            sumEvnets1 ++;
        }

    }
    for(int ii=0; ii<t_data->GetEntries(); ii++)
    {
        t_data->GetEntry(ii);
        if(sumEvnets2 != numEvent)
        {
            maxevent2 = numEvent;
            sumEvnets2 ++;
        }

    }
    
    cout << t_cut->GetName() << "   sum of Events: " << sumEvnets1 << endl;
    cout << t_cut->GetEntries() << endl;
    cout << t_data->GetName() << "  sum of Events: " << sumEvnets2 << endl;
    cout << t_data->GetEntries() << endl;

    ouput->Close();
    //ofcut.close();
    delete ouput;
}

